local skel = fk.CreateSkill {
  name = "emo__yueding",
  tags = {Skill.Limited},
}

Fk:loadTranslationTable{
  ["emo__yueding"] = "约定",
  [":emo__yueding"] = "限定技，回合开始时，若“监视”目标死亡，你可令本回合使用伤害牌无次数距离和目标数限制，本回合结束时，令其复活并恢复体力至你本回合造成伤害数。",

  ["@@emo__yueding-turn"] = "<font color='red'>约定</font>",
  ["#emo__yueding-ask"] = "约定：你可以令你伤害牌无次数距离和目标数限制，并于回合结束时复活“监视”目标！",
}

skel:addEffect(fk.TurnStart, {
  can_trigger = function (self, event, target, player, data)
    local mate = player.room:getPlayerById(player:getMark("@[chara]emo__jianshi"))
    if not mate then return false end
    if player:hasSkill(skel.name) and target == player then
      return mate.dead and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#emo__yueding-ask" })
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@@emo__yueding-turn", 1)
  end,
})

skel:addEffect(fk.TurnEnd, {
  can_trigger = function (self, event, target, player, data)
    return player:getMark("@@emo__yueding-turn") ~= 0 and target == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local mate = room:getPlayerById(player:getMark("@[chara]emo__jianshi"))
    if not mate then return false end
    local n = 0
    player.room.logic:getActualDamageEvents(1, function(e)
      if e.data.from == player then
        n = n + e.data.damage
      end
    end)
    n = math.max(1, n)
    room:revivePlayer(mate, true, skel.name)
    if mate.dead then return end
    if mate.maxHp < 1 then
      room:setPlayerProperty(mate, "maxHp", 1)
    end
    room:setPlayerProperty(mate, "hp", n)
  end,
})

skel:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    return card and card.is_damage_card and player:getMark("@@emo__yueding-turn") ~= 0
  end,
  bypass_times = function (self, player, skill, scope, card, to)
    return card and card.is_damage_card and player:getMark("@@emo__yueding-turn") ~= 0
  end,
  extra_target_func = function (self, player, skill, card)
    if card and card.is_damage_card and player:getMark("@@emo__yueding-turn") ~= 0 then
      return 999
    end
  end,
})

return skel
